//
//  SaleAfter-list.swift
//  smosky
//
//  Created by zhuxietong on 2018/8/9.
//  Copyright © 2018年 tianrun. All rights reserved.
//

import Foundation
import soapp
import Eelay
import AsyncDisplayKit
extension SaleAfter{
    
    class ListCell:JoCellNode,EeActionDelegate{
        
        func active(onwer: Any, actionID: String, data: NSMutableDictionary) {
           
            Goods.seeDetail(obj: data, at: content_controller)

        }
        
        class StatusBar: MoDisplayNode {
            let iconV = ASImageNode()
            let nameL = ASTextNode2()
            var status = AfterStatus.商家同意退货{
                didSet{
//                    iconV.image = UIImage(named: status.afterType.icon)
//                    nameL.attributedText = status.baseName.ui.font13.cl_999.astring
                }
            }
            override func addLayoutRules() {
            }
            override func mo_model_reload() {
                let afterType = SaleAfterType(rawValue: mo.model["refund.rtype",""]) ?? .returnMoney
                iconV.image = UIImage(named: afterType.icon)
                nameL.attributedText = mo.model["refund.refundstatus",""].ui.font13.color(color: ._weak).astring
            }

            override func specGetter() -> _Spec {
                return _Spec.stack(["h|10",&-.center,[10,AppTheme.edge],_nowrap], childs: [
                    iconV["20",21],nameL[_shrink(1),[2,0,0,0]]
                ])
            }
        }
        
        //详情页没有顶部空白
        class SellerNode1: Goods.OrderListCell.SellerNode {
            override func mo_model_reload() {
                logoV.img_url = mo.model["logo",__heads.one_str]
                titleL.attributedText = mo.model["merchname",__names.one_str].ui.font14.color(color: ._foreground).astring
                line.backgroundColor = ._separator
                topline.backgroundColor = .color(light: "#fff", dark: "#fff0")
            }
            
            override func specGetter() -> _Spec {
                
                let content:_Spec = .stack(["h|10",_nowrap,&-.center,_justfy(.space_between)], childs: [
                    _Spec.stack(["h|8",_shrink(1)], childs: [
                        logoV["20",20],
                        titleL.Spec
                        ]),
                    statusL[_shrink(1)],
                    ])
                
                return .stack(["v",&-.stretch], childs: [
//                    topline["2"],
                    .inset([], [8,AppTheme.edge], child: content),
                    line["\(TP.separatorHeight)"],
                    ])
            }
        }
        
        let goodsV = JoRuleNode<Goods.OrderListCell.GoodsKit>()
        let sellerV = SellerNode1()
        let statusBar = StatusBar()
        let detailBt = ASButtonNode()
        
        let line = ASDisplayNode()
        var status = AfterStatus.商家同意退货{
            didSet{
                statusBar.status = status
            }
        }
        
        override func addLayoutRules() {
            super.addLayoutRules()

            view.backgroundColor = ._cellBackground
        }
        
        override func cellTouch() {
            var model = mo.model
            if let __model = DataNode<NSMutableDictionary>.path(node: "__model", mo.model)
            {
                model = __model
            }
            let ctr = SaleAfterDetailCtr()
            ctr.mo.model = model
            mo.controller?.navigationController?.pushViewController(ctr, animated: true)
        }
        override func mo_model_reload() {
            
            super.mo_model_reload()
            internalCellTouch = true
            var model = mo.model
            if let __model = DataNode<NSMutableDictionary>.path(node: "__model", mo.model)
            {
                
                model = __model
            }
            
            goodsV.containerConfig = {
                spec in
                spec.alignItems = ASStackLayoutAlignItems.stretch
                spec.direction = ASStackLayoutDirection.vertical
            }
            let goods = DataNode.path(node: "refund.goods", model, value: NSMutableArray())
            if let last = goods.lastObject as? NSMutableDictionary{
                last["__last__"] = FK.YES
            }
            goodsV.nodePath = "refund.goods"
            goodsV.mo.model = model
            goodsV.space = 0
            backgroundColor = ._cellBackground
            goodsV.delegate = self
            var num = 0
            goods.list { (one:NSMutableDictionary, i) in
                let one_num = one[int:"total",0]
                num = num + one_num
            }
            
            
            statusBar.mo.model = mo.model
            self.status = AfterStatus(rawValue: mo.model["refund.goods.0.rstate",""]) ?? .退款完成
            
            sellerV.mo.model = mo.model
//            statusBar.backgroundColor = UIColor(shex: "#F8F8F8")
            statusBar.backgroundColor = .color(light: "#f8f8f8", dark: "#202020")

            detailBt.ui.font(size: 12).ncolor(color: ._tint).ntitle(title: "查看详情".loc).padding(insets: [6,12]).set(radius: 4, width: 1, color: ._tint)
            line.backgroundColor = UIColor.table_bg_color
//            separatorHeight = 10
        }
        
        var orderID:String{
            get{
                var model = mo.model
                if let __model = DataNode<NSMutableDictionary>.path(node: "__model", mo.model)
                {
                    model = __model
                }
                let id = model["id",""]
                return id
            }
        }
        
        
        override func specGetter() -> _Spec {
            
            let list: [_Spec] = [
                line["8"],
                sellerV.Spec,
                goodsV.Spec,
                statusBar.Spec,
                .stack(["h",_justfy(.flex_end),[8,AppTheme.edge]], childs: [detailBt.Spec])
            ]
//
//            if let bar = self.actionBarSpec
//            {
//                list.append(bar)
//            }
            let spec = _Spec.stack(["v",_shrink(1),_justfy(.space_between),&-.stretch], childs: list)
            return spec
            
        }
    }
    
}
